package com.bazaaroid.mobile.android.helper;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

import com.bazaaroid.mobile.rest.model.partner.*;

public class BazaaroidXmlParser {

	public static String TAG = "[BazaaroidXmlParser]";
	DocumentBuilderFactory docBuilderFactory = null;
	
	public BazaaroidXmlParser()
	{
		try
		{
			docBuilderFactory = DocumentBuilderFactory.newInstance();
		}
		catch(Exception exc)
		{
			Log.e(TAG, String.format("Exception occurred while instantiating DocumentBuilderFactory.\nMessage: %s", 
					(exc.getMessage() == null) ? "NULL" : exc.getMessage()));
		}
	}
	
	public ArrayList<CategoryItem> parseCategories(InputStream xmlCategories)
	{
		ArrayList<CategoryItem> categories = new ArrayList<CategoryItem>();
		
		try
		{
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document document = docBuilder.parse(xmlCategories);
			Element rootElement = document.getDocumentElement();
			NodeList categoryItems = rootElement.getElementsByTagName("category");
			for(int i = 0; i < categoryItems.getLength(); ++i)
			{
				CategoryItem category = new CategoryItem();
				Node categoryItem = categoryItems.item(i);
				NodeList categoryFields = categoryItem.getChildNodes();
				for(int j = 0; j < categoryFields.getLength(); ++j)
				{
					Node categoryField = categoryFields.item(j);
					String categoryFieldName = categoryField.getNodeName();
					if(categoryFieldName.equalsIgnoreCase(CategoryItem.KEY))
					{
						category.setKey(categoryField.getFirstChild().getNodeValue());
					}
					else if(categoryFieldName.equalsIgnoreCase(CategoryItem.NAME))
					{
						category.setName(categoryField.getFirstChild().getNodeValue());
					}
				}
				
				categories.add(category);
			}
		}
		catch(Exception exc)
		{
			Log.e(TAG, String.format("Exception occurred while parsing categories.\nMessage: %s", 
					(exc.getMessage() == null) ? "NULL" : exc.getMessage()));	
		}
		
		
		// TODO: to implement
		return categories;
	}
	public ArrayList<LocationItem> parseLocations(InputStream xmlLocations)
	{
		ArrayList<LocationItem> locations = new ArrayList<LocationItem>();
		
		try
		{
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document document = docBuilder.parse(xmlLocations);
			Element rootElement = document.getDocumentElement();
			NodeList locationItems = rootElement.getElementsByTagName("location");
			for(int i = 0; i < locationItems.getLength(); ++i)
			{
				LocationItem location = new LocationItem();
				Node locationItem = locationItems.item(i);
				NodeList locationFields = locationItem.getChildNodes();
				for(int j = 0; j < locationFields.getLength(); ++j)
				{
					Node locationField = locationFields.item(j);
					String locationFieldName = locationField.getNodeName();
					if(locationFieldName.equalsIgnoreCase(LocationItem.KEY))
					{
						location.setKey(locationField.getFirstChild().getNodeValue());
					}
					else if(locationFieldName.equalsIgnoreCase(LocationItem.NAME))
					{
						location.setName(locationField.getFirstChild().getNodeValue());
					}
					else if(locationFieldName.equalsIgnoreCase(LocationItem.ADDRESS))
					{
						//Node addressNode = locationField.getFirstChild();
						NodeList addressFields = locationField.getChildNodes();
						
						AddressItem address = new AddressItem();
						
						for(int k = 0; k < addressFields.getLength(); ++k)
						{
							Node addressField = addressFields.item(k);
							Node addressFieldChild = addressField.getFirstChild();
							if(addressFieldChild != null)
							{
								String addressFieldName = addressField.getNodeName();
								
								if(addressFieldName.equalsIgnoreCase(AddressItem.CITY))
								{
									address.setCity(addressField.getFirstChild().getNodeValue());
								}
								else if(addressFieldName.equalsIgnoreCase(AddressItem.COUNTRY))
								{
									address.setCountry(addressField.getFirstChild().getNodeValue());								
								}
								else if(addressFieldName.equalsIgnoreCase(AddressItem.NUMBER))
								{
									address.setNumber(addressField.getFirstChild().getNodeValue());
								}
								else if(addressFieldName.equalsIgnoreCase(AddressItem.POSTAL_CODE))
								{
									address.setPostalCode(addressField.getFirstChild().getNodeValue());
								}
								else if(addressFieldName.equalsIgnoreCase(AddressItem.STATE))
								{
									address.setState(addressField.getFirstChild().getNodeValue());
								}
								else if(addressFieldName.equalsIgnoreCase(AddressItem.STREET))
								{
									address.setStreet(addressField.getFirstChild().getNodeValue());
								}
								else if(addressFieldName.equalsIgnoreCase(AddressItem.TOWNSHIP))
								{
									address.setTownship(addressField.getFirstChild().getNodeValue());
								}
							}
						}
						
						location.setAddress(address);
					}
					else if(locationFieldName.equalsIgnoreCase(LocationItem.GEO_POINT))
					{
						//Node geoPtNode = locationField.getFirstChild();
						NodeList geoPtFields = locationField.getChildNodes();
						
						GeoPointItem geoPoint = new GeoPointItem();
						
						for(int k = 0; k < geoPtFields.getLength(); ++k)
						{
							Node geoPtField = geoPtFields.item(k);
							String geoPtFieldName = geoPtField.getNodeName();
							if(geoPtFieldName.equalsIgnoreCase(GeoPointItem.LATITUDE))
							{
								geoPoint.setLatitude(Double.parseDouble(geoPtField.getFirstChild().getNodeValue()));
							}
							else if(geoPtFieldName.equalsIgnoreCase(GeoPointItem.LONGITUDE))
							{
								geoPoint.setLongitude(Double.parseDouble(geoPtField.getFirstChild().getNodeValue()));
							}
						}
						location.setGeoPt(geoPoint);
					}
					else if(locationFieldName.equalsIgnoreCase(LocationItem.OPENING_HRS))
					{
						location.setOpeningHours(locationField.getFirstChild().getNodeValue());
					}
				}
				
				locations.add(location);
			}
		}
		catch(Exception exc)
		{
			Log.e(TAG, String.format("Exception occurred while parsing locations.\nMessage: %s", 
					(exc.getMessage() == null) ? "NULL" : exc.getMessage()));	
		}
		
		return locations;
	}
	
	public ArrayList<ActiveDiscountItem> parseActiveDiscounts(InputStream xmlActiveDiscounts)
	{
		ArrayList<ActiveDiscountItem> discounts = new ArrayList<ActiveDiscountItem>();
		
		try
		{
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document document = docBuilder.parse(xmlActiveDiscounts);
			Element rootElement = document.getDocumentElement();
			NodeList discountItems = rootElement.getElementsByTagName("discount");
			for(int i = 0; i < discountItems.getLength(); ++i)
			{
				ActiveDiscountItem discount = new ActiveDiscountItem();
				Node discountItem = discountItems.item(i);
				NodeList discountFields = discountItem.getChildNodes();
				for(int j = 0; j < discountFields.getLength(); ++j)
				{
					Node discountField = discountFields.item(j);
					String discountFieldName = discountField.getNodeName();
					if(discountFieldName.equalsIgnoreCase(ActiveDiscountItem.KEY))
					{
						discount.setKey(discountField.getFirstChild().getNodeValue());
					}
					else if(discountFieldName.equalsIgnoreCase(ActiveDiscountItem.PERCENTAGE))
					{
						discount.setPercentage(discountField.getFirstChild().getNodeValue());
					}
					else if(discountFieldName.equalsIgnoreCase(ActiveDiscountItem.PRICE_WITH_DISCOUNT))
					{
						discount.setPriceWithDiscount(discountField.getFirstChild().getNodeValue());
					}
					else if(discountFieldName.equalsIgnoreCase(ActiveDiscountItem.PRODUCT))
					{
						// TODO:
						NodeList productFields = discountField.getChildNodes();
						
						ProductItem product = new ProductItem();
						
						for(int k = 0; k < productFields.getLength(); ++k)
						{
							Node productField = productFields.item(k);
							Node productFieldChild = productField.getFirstChild();
							String productFieldName = productField.getNodeName();
							
							if(productFieldChild != null)
							{
								if(productFieldName.equalsIgnoreCase(ProductItem.KEY))
								{
									product.setKey(productField.getFirstChild().getNodeValue());
								}
								else if(productFieldName.equalsIgnoreCase(ProductItem.NAME))
								{
									product.setName(productField.getFirstChild().getNodeValue());
								}
								else if(productFieldName.equalsIgnoreCase(ProductItem.DESC))
								{
									product.setDescription(productField.getFirstChild().getNodeValue());
								}
								else if(productFieldName.equalsIgnoreCase(ProductItem.PRICE))
								{
									product.setPrice(productField.getFirstChild().getNodeValue());
								}
								else if(productFieldName.equalsIgnoreCase(ProductItem.CURRENCY))
								{
									product.setCurrency(productField.getFirstChild().getNodeValue());
								}
								else if(productFieldName.equalsIgnoreCase(ProductItem.DISCOUNT))
								{
									NodeList productDiscountFields = productField.getChildNodes();
									
									DiscountItem productDiscount = new DiscountItem();
									
									for(int kk = 0; kk < discountFields.getLength(); ++kk)
									{
										Node productDiscountField = discountFields.item(kk);
										Node productDiscountFieldChild = productDiscountField.getFirstChild();
										String productDiscountFieldName = productDiscountField.getNodeName();
										
										if(productDiscountFieldChild != null)
										{
											if(productDiscountFieldName.equalsIgnoreCase(DiscountItem.KEY))
											{
												productDiscount.setKey(productDiscountField.getFirstChild().getNodeValue());
											}
											else if(productDiscountFieldName.equalsIgnoreCase(DiscountItem.NAME))
											{
												productDiscount.setName(productDiscountField.getFirstChild().getNodeValue());
											}
											else if(productDiscountFieldName.equalsIgnoreCase(DiscountItem.DESC))
											{
												productDiscount.setDescription(productDiscountField.getFirstChild().getNodeValue());
											}
											else if(productDiscountFieldName.equalsIgnoreCase(DiscountItem.PERCENTAGE))
											{
												productDiscount.setPercentage(productDiscountField.getFirstChild().getNodeValue());
											}
											else if(productDiscountFieldName.equalsIgnoreCase(DiscountItem.PRICE_WITH_DISC))
											{
												productDiscount.setPriceWithDiscount(productDiscountField.getFirstChild().getNodeValue());
											}
											else if(productDiscountFieldName.equalsIgnoreCase(DiscountItem.TIME_PERIOD))
											{
												NodeList timePeriodFields = productDiscountField.getChildNodes();
												
												TimePeriodItem timePeriod = new TimePeriodItem();
												
												for(int l = 0; l < timePeriodFields.getLength(); ++l)
												{
													Node timePeriodField = timePeriodFields.item(l);
													Node timePeriodFieldChild = timePeriodField.getFirstChild();
													if(timePeriodFieldChild != null)
													{
														String timePeriodFieldName = timePeriodField.getNodeName();
														if(timePeriodFieldName.equalsIgnoreCase(TimePeriodItem.DATE_FROM))
														{
															timePeriod.setDateFrom(timePeriodField.getFirstChild().getNodeValue());
														}
														else if(timePeriodFieldName.equalsIgnoreCase(TimePeriodItem.DATE_TO))
														{
															timePeriod.setDateTo(timePeriodField.getFirstChild().getNodeValue());
														}
													}
												}
												productDiscount.setTimePeriod(timePeriod);
											}
										}
									}
									
									product.setDiscount(productDiscount);
								}
								else if(productFieldName.equalsIgnoreCase(ProductItem.PARTNER))
								{
									NodeList partnerFields = productField.getChildNodes();
									
									PartnerItem partner = new PartnerItem();
									
									for(int l = 0; l < partnerFields.getLength(); ++l)
									{
										Node partnerField = partnerFields.item(l);
										Node partnerFieldChild = partnerField.getFirstChild();
										String partnerFieldName = partnerField.getNodeName();
										
										if(partnerFieldChild != null)
										{
											if(partnerFieldName.equalsIgnoreCase(PartnerItem.KEY))
											{
												partner.setKey(partnerField.getFirstChild().getNodeValue());
											}
											else if(partnerFieldName.equalsIgnoreCase(PartnerItem.NAME))
											{
												partner.setName(partnerField.getFirstChild().getNodeValue());
											}
											else if(partnerFieldName.equalsIgnoreCase(PartnerItem.DESC))
											{
												partner.setDescription(partnerField.getFirstChild().getNodeValue());
											}
											else if(partnerFieldName.equalsIgnoreCase(PartnerItem.CONTACT))
											{
												NodeList contactFields = partnerField.getChildNodes();
												
												ContactItem contact = new ContactItem();
												
												for(int ll = 0; ll < contactFields.getLength(); ++ll)
												{
													Node contactField = contactFields.item(ll);
													Node contactFieldChild = contactField.getFirstChild();
													if(contactFieldChild != null)
													{
														String timePeriodFieldName = contactField.getNodeName();
														if(timePeriodFieldName.equalsIgnoreCase(ContactItem.EMAIL))
														{
															contact.setEmail(contactField.getFirstChild().getNodeValue());
														}
														else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.FAX))
														{
															contact.setFax(contactField.getFirstChild().getNodeValue());
														}
														else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.MOBILE))
														{
															contact.setMobile(contactField.getFirstChild().getNodeValue());
														}
														else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.SITE))
														{
															contact.setSite(contactField.getFirstChild().getNodeValue());
														}
														else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.TELEPHONE))
														{
															contact.setTelephone(contactField.getFirstChild().getNodeValue());
														}
													}
												}
												partner.setContact(contact);
											}
										}
									}
									
									product.setPartner(partner);
								}
								else if(productFieldName.equalsIgnoreCase(ProductItem.CATEGORIES))
								{
									NodeList categories = productField.getChildNodes();
									
									ArrayList<CategoryItem> categoryItems = new ArrayList<CategoryItem>();
									
									for(int kk = 0; kk < categories.getLength(); ++kk)
									{
										Node category = categories.item(kk);
										CategoryItem categoryItem = new CategoryItem();
										NodeList categoryFields = category.getChildNodes();
										
										for(int l = 0; l < categoryFields.getLength(); ++l)
										{
											Node categoryField = categoryFields.item(l);
											Node categoryFieldChild = categoryField.getFirstChild();
											String categoryFieldName = categoryField.getNodeName();
											
											if(categoryFieldChild != null)
											{
												if(categoryFieldName.equalsIgnoreCase(CategoryItem.KEY))
												{
													categoryItem.setKey(categoryField.getFirstChild().getNodeValue());
												}
												else if(categoryFieldName.equalsIgnoreCase(CategoryItem.NAME))
												{
													categoryItem.setName(categoryField.getFirstChild().getNodeValue());
												}
											}
										}
										categoryItems.add(categoryItem);
									}
									product.setCategories(categoryItems);
								}
								else if(productFieldName.equalsIgnoreCase(ProductItem.TAGS))
								{
									NodeList tags = productField.getChildNodes();
									
									ArrayList<TagItem> tagItems = new ArrayList<TagItem>();
									
									for(int kk = 0; kk < tags.getLength(); ++kk)
									{
										Node tag = tags.item(kk);
										TagItem tagItem = new TagItem();
										NodeList tagFields = tag.getChildNodes();
										
										for(int l = 0; l < tagFields.getLength(); ++l)
										{
											Node tagField = tagFields.item(l);
											Node tagFieldChild = tagField.getFirstChild();
											String tagFieldName = tagField.getNodeName();
											
											if(tagFieldChild != null)
											{
												if(tagFieldName.equalsIgnoreCase(CategoryItem.KEY))
												{
													tagItem.setKey(tagField.getFirstChild().getNodeValue());
												}
												else if(tagFieldName.equalsIgnoreCase(CategoryItem.NAME))
												{
													tagItem.setName(tagField.getFirstChild().getNodeValue());
												}
											}
										}
										tagItems.add(tagItem);
									}
									product.setTags(tagItems);
								}
								else if(productFieldName.equalsIgnoreCase(ProductItem.LOCATIONS))
								{
									NodeList locations = productField.getChildNodes();
									
									ArrayList<LocationItem> locationItems = new ArrayList<LocationItem>();
									
									for(int kk = 0; kk < locations.getLength(); ++kk)
									{
										Node location = locations.item(kk);
										LocationItem locationItem = new LocationItem();
										NodeList locationFields = location.getChildNodes();
										
										for(int l = 0; l < locationFields.getLength(); ++l)
										{
											Node locationField = locationFields.item(l);
											Node locationFieldChild = locationField.getFirstChild();
											String locationFieldName = locationField.getNodeName();
											
											if(locationFieldChild != null)
											{
												if(locationFieldName.equalsIgnoreCase(LocationItem.KEY))
												{
													locationItem.setKey(locationField.getFirstChild().getNodeValue());
												}
												else if(locationFieldName.equalsIgnoreCase(LocationItem.NAME))
												{
													locationItem.setName(locationField.getFirstChild().getNodeValue());
												}
												else if(locationFieldName.equalsIgnoreCase(LocationItem.ADDRESS))
												{
													//Node addressNode = locationField.getFirstChild();
													NodeList addressFields = locationField.getChildNodes();
													
													AddressItem address = new AddressItem();
													
													for(int m = 0; m < addressFields.getLength(); ++m)
													{
														Node addressField = addressFields.item(m);
														Node addressFieldChild = addressField.getFirstChild();
														if(addressFieldChild != null)
														{
															String addressFieldName = addressField.getNodeName();
															
															if(addressFieldName.equalsIgnoreCase(AddressItem.CITY))
															{
																address.setCity(addressField.getFirstChild().getNodeValue());
															}
															else if(addressFieldName.equalsIgnoreCase(AddressItem.COUNTRY))
															{
																address.setCountry(addressField.getFirstChild().getNodeValue());								
															}
															else if(addressFieldName.equalsIgnoreCase(AddressItem.NUMBER))
															{
																address.setNumber(addressField.getFirstChild().getNodeValue());
															}
															else if(addressFieldName.equalsIgnoreCase(AddressItem.POSTAL_CODE))
															{
																address.setPostalCode(addressField.getFirstChild().getNodeValue());
															}
															else if(addressFieldName.equalsIgnoreCase(AddressItem.STATE))
															{
																address.setState(addressField.getFirstChild().getNodeValue());
															}
															else if(addressFieldName.equalsIgnoreCase(AddressItem.STREET))
															{
																address.setStreet(addressField.getFirstChild().getNodeValue());
															}
															else if(addressFieldName.equalsIgnoreCase(AddressItem.TOWNSHIP))
															{
																address.setTownship(addressField.getFirstChild().getNodeValue());
															}
														}
													}
													
													locationItem.setAddress(address);
												}
												else if(locationFieldName.equalsIgnoreCase(LocationItem.GEO_POINT))
												{
													//Node geoPtNode = locationField.getFirstChild();
													NodeList geoPtFields = locationField.getChildNodes();
													
													GeoPointItem geoPoint = new GeoPointItem();
													
													for(int m = 0; m < geoPtFields.getLength(); ++m)
													{
														Node geoPtField = geoPtFields.item(m);
														String geoPtFieldName = geoPtField.getNodeName();
														if(geoPtFieldName.equalsIgnoreCase(GeoPointItem.LATITUDE))
														{
															geoPoint.setLatitude(Double.parseDouble(geoPtField.getFirstChild().getNodeValue()));
														}
														else if(geoPtFieldName.equalsIgnoreCase(GeoPointItem.LONGITUDE))
														{
															geoPoint.setLongitude(Double.parseDouble(geoPtField.getFirstChild().getNodeValue()));
														}
													}
													locationItem.setGeoPt(geoPoint);
												}
												else if(locationFieldName.equalsIgnoreCase(LocationItem.OPENING_HRS))
												{
													locationItem.setOpeningHours(locationField.getFirstChild().getNodeValue());
												}
											}
										}
										locationItems.add(locationItem);
									}
									product.setLocations(locationItems);
								}
							}
						}
						
						discount.setProduct(product);
					}
				}
				
				discounts.add(discount);
			}
		}
		catch(Exception exc)
		{
			Log.e(TAG, String.format("Exception occurred while parsing active discounts.\nMessage: %s", 
					(exc.getMessage() == null) ? "NULL" : exc.getMessage()));	
		}
		
		return discounts;
	}
	
	public ArrayList<DiscountItem> parseDiscounts(InputStream xmlDiscounts)
	{
		ArrayList<DiscountItem> discounts = new ArrayList<DiscountItem>();
		
		try
		{
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document document = docBuilder.parse(xmlDiscounts);
			Element rootElement = document.getDocumentElement();
			NodeList discountItems = rootElement.getElementsByTagName("discount");
			for(int i = 0; i < discountItems.getLength(); ++i)
			{
				DiscountItem discount = new DiscountItem();
				Node discountItem = discountItems.item(i);
				NodeList discountFields = discountItem.getChildNodes();
				for(int j = 0; j < discountFields.getLength(); ++j)
				{
					Node discountField = discountFields.item(j);
					String discountFieldName = discountField.getNodeName();
					if(discountFieldName.equalsIgnoreCase(DiscountItem.KEY))
					{
						discount.setKey(discountField.getFirstChild().getNodeValue());
					}
					else if(discountFieldName.equalsIgnoreCase(DiscountItem.NAME))
					{
						discount.setName(discountField.getFirstChild().getNodeValue());
					}
					else if(discountFieldName.equalsIgnoreCase(DiscountItem.DESC))
					{
						discount.setDescription(discountField.getFirstChild().getNodeValue());
					}
					else if(discountFieldName.equalsIgnoreCase(DiscountItem.PERCENTAGE))
					{
						discount.setPercentage(discountField.getFirstChild().getNodeValue());
					}
					else if(discountFieldName.equalsIgnoreCase(DiscountItem.PRICE_WITH_DISC))
					{
						discount.setPriceWithDiscount(discountField.getFirstChild().getNodeValue());
					}
					else if(discountFieldName.equalsIgnoreCase(DiscountItem.TIME_PERIOD))
					{
						NodeList timePeriodFields = discountField.getChildNodes();
						
						TimePeriodItem timePeriod = new TimePeriodItem();
						
						for(int k = 0; k < timePeriodFields.getLength(); ++k)
						{
							Node timePeriodField = timePeriodFields.item(k);
							Node timePeriodFieldChild = timePeriodField.getFirstChild();
							if(timePeriodFieldChild != null)
							{
								String timePeriodFieldName = timePeriodField.getNodeName();
								if(timePeriodFieldName.equalsIgnoreCase(TimePeriodItem.DATE_FROM))
								{
									timePeriod.setDateFrom(timePeriodField.getFirstChild().getNodeValue());
								}
								else if(timePeriodFieldName.equalsIgnoreCase(TimePeriodItem.DATE_TO))
								{
									timePeriod.setDateTo(timePeriodField.getFirstChild().getNodeValue());
								}
							}
						}
						discount.setTimePeriod(timePeriod);
					}
				}
				
				discounts.add(discount);
			}
		}
		catch(Exception exc)
		{
			Log.e(TAG, String.format("Exception occurred while parsing discounts.\nMessage: %s", 
					(exc.getMessage() == null) ? "NULL" : exc.getMessage()));	
		}
		
		return discounts;
	}
	
	public ArrayList<TagItem> parseTags(InputStream xmlTags)
	{
		ArrayList<TagItem> tags = new ArrayList<TagItem>();
		
		try
		{
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document document = docBuilder.parse(xmlTags);
			Element rootElement = document.getDocumentElement();
			NodeList tagItems = rootElement.getElementsByTagName("tag");
			for(int i = 0; i < tagItems.getLength(); ++i)
			{
				TagItem tag = new TagItem();
				Node tagItem = tagItems.item(i);
				NodeList tagFields = tagItem.getChildNodes();
				for(int j = 0; j < tagFields.getLength(); ++j)
				{
					Node tagField = tagFields.item(j);
					String tagFieldName = tagField.getNodeName();
					if(tagFieldName.equalsIgnoreCase(DiscountItem.KEY))
					{
						tag.setKey(tagField.getFirstChild().getNodeValue());
					}
					else if(tagFieldName.equalsIgnoreCase(DiscountItem.NAME))
					{
						tag.setName(tagField.getFirstChild().getNodeValue());
					}
				}
				
				tags.add(tag);
			}
		}
		catch(Exception exc)
		{
			Log.e(TAG, String.format("Exception occurred while parsing tags.\nMessage: %s", 
					(exc.getMessage() == null) ? "NULL" : exc.getMessage()));	
		}
		
		return tags;
	}
	
	public ArrayList<ProductItem> parseProducts(InputStream xmlProducts)
	{
		ArrayList<ProductItem> products = new ArrayList<ProductItem>();
		
		try
		{
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document document = docBuilder.parse(xmlProducts);
			Element rootElement = document.getDocumentElement();
			NodeList productItems = rootElement.getElementsByTagName("product");
			for(int i = 0; i < productItems.getLength(); ++i)
			{
				ProductItem product = new ProductItem();
				Node productItem = productItems.item(i);
				NodeList productFields = productItem.getChildNodes();
				for(int j = 0; j < productFields.getLength(); ++j)
				{
					Node productField = productFields.item(j);
					String productFieldName = productField.getNodeName();
					if(productFieldName.equalsIgnoreCase(ProductItem.KEY))
					{
						product.setKey(productField.getFirstChild().getNodeValue());
					}
					else if(productFieldName.equalsIgnoreCase(ProductItem.NAME))
					{
						product.setName(productField.getFirstChild().getNodeValue());
					}
					else if(productFieldName.equalsIgnoreCase(ProductItem.DESC))
					{
						product.setDescription(productField.getFirstChild().getNodeValue());
					}
					else if(productFieldName.equalsIgnoreCase(ProductItem.PRICE))
					{
						product.setPrice(productField.getFirstChild().getNodeValue());
					}
					else if(productFieldName.equalsIgnoreCase(ProductItem.CURRENCY))
					{
						product.setCurrency(productField.getFirstChild().getNodeValue());
					}
					else if(productFieldName.equalsIgnoreCase(ProductItem.DISCOUNT))
					{
						NodeList discountFields = productField.getChildNodes();
						
						DiscountItem discount = new DiscountItem();
						
						for(int k = 0; k < discountFields.getLength(); ++k)
						{
							Node discountField = discountFields.item(k);
							Node discountFieldChild = discountField.getFirstChild();
							String discountFieldName = discountField.getNodeName();
							
							if(discountFieldChild != null)
							{
								if(discountFieldName.equalsIgnoreCase(DiscountItem.KEY))
								{
									discount.setKey(discountField.getFirstChild().getNodeValue());
								}
								else if(discountFieldName.equalsIgnoreCase(DiscountItem.NAME))
								{
									discount.setName(discountField.getFirstChild().getNodeValue());
								}
								else if(discountFieldName.equalsIgnoreCase(DiscountItem.DESC))
								{
									discount.setDescription(discountField.getFirstChild().getNodeValue());
								}
								else if(discountFieldName.equalsIgnoreCase(DiscountItem.PERCENTAGE))
								{
									discount.setPercentage(discountField.getFirstChild().getNodeValue());
								}
								else if(discountFieldName.equalsIgnoreCase(DiscountItem.PRICE_WITH_DISC))
								{
									discount.setPriceWithDiscount(discountField.getFirstChild().getNodeValue());
								}
								else if(discountFieldName.equalsIgnoreCase(DiscountItem.TIME_PERIOD))
								{
									NodeList timePeriodFields = discountField.getChildNodes();
									
									TimePeriodItem timePeriod = new TimePeriodItem();
									
									for(int l = 0; l < timePeriodFields.getLength(); ++l)
									{
										Node timePeriodField = timePeriodFields.item(l);
										Node timePeriodFieldChild = timePeriodField.getFirstChild();
										if(timePeriodFieldChild != null)
										{
											String timePeriodFieldName = timePeriodField.getNodeName();
											if(timePeriodFieldName.equalsIgnoreCase(TimePeriodItem.DATE_FROM))
											{
												timePeriod.setDateFrom(timePeriodField.getFirstChild().getNodeValue());
											}
											else if(timePeriodFieldName.equalsIgnoreCase(TimePeriodItem.DATE_TO))
											{
												timePeriod.setDateTo(timePeriodField.getFirstChild().getNodeValue());
											}
										}
									}
									discount.setTimePeriod(timePeriod);
								}
							}
						}
						
						product.setDiscount(discount);
					}
					else if(productFieldName.equalsIgnoreCase(ProductItem.PARTNER))
					{
						NodeList partnerFields = productField.getChildNodes();
						
						PartnerItem partner = new PartnerItem();
						
						for(int k = 0; k < partnerFields.getLength(); ++k)
						{
							Node partnerField = partnerFields.item(k);
							Node partnerFieldChild = partnerField.getFirstChild();
							String partnerFieldName = partnerField.getNodeName();
							
							if(partnerFieldChild != null)
							{
								if(partnerFieldName.equalsIgnoreCase(PartnerItem.KEY))
								{
									partner.setKey(partnerField.getFirstChild().getNodeValue());
								}
								else if(partnerFieldName.equalsIgnoreCase(PartnerItem.NAME))
								{
									partner.setName(partnerField.getFirstChild().getNodeValue());
								}
								else if(partnerFieldName.equalsIgnoreCase(PartnerItem.DESC))
								{
									partner.setDescription(partnerField.getFirstChild().getNodeValue());
								}
								else if(partnerFieldName.equalsIgnoreCase(PartnerItem.CONTACT))
								{
									NodeList contactFields = partnerField.getChildNodes();
									
									ContactItem contact = new ContactItem();
									
									for(int l = 0; l < contactFields.getLength(); ++l)
									{
										Node contactField = contactFields.item(l);
										Node contactFieldChild = contactField.getFirstChild();
										if(contactFieldChild != null)
										{
											String timePeriodFieldName = contactField.getNodeName();
											if(timePeriodFieldName.equalsIgnoreCase(ContactItem.EMAIL))
											{
												contact.setEmail(contactField.getFirstChild().getNodeValue());
											}
											else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.FAX))
											{
												contact.setFax(contactField.getFirstChild().getNodeValue());
											}
											else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.MOBILE))
											{
												contact.setMobile(contactField.getFirstChild().getNodeValue());
											}
											else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.SITE))
											{
												contact.setSite(contactField.getFirstChild().getNodeValue());
											}
											else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.TELEPHONE))
											{
												contact.setTelephone(contactField.getFirstChild().getNodeValue());
											}
										}
									}
									partner.setContact(contact);
								}
							}
						}
						
						product.setPartner(partner);
					}
					else if(productFieldName.equalsIgnoreCase(ProductItem.CATEGORIES))
					{
						NodeList categories = productField.getChildNodes();
						
						ArrayList<CategoryItem> categoryItems = new ArrayList<CategoryItem>();
						
						for(int k = 0; k < categories.getLength(); ++k)
						{
							Node category = categories.item(k);
							CategoryItem categoryItem = new CategoryItem();
							NodeList categoryFields = category.getChildNodes();
							
							for(int l = 0; l < categoryFields.getLength(); ++l)
							{
								Node categoryField = categoryFields.item(l);
								Node categoryFieldChild = categoryField.getFirstChild();
								String categoryFieldName = categoryField.getNodeName();
								
								if(categoryFieldChild != null)
								{
									if(categoryFieldName.equalsIgnoreCase(CategoryItem.KEY))
									{
										categoryItem.setKey(categoryField.getFirstChild().getNodeValue());
									}
									else if(categoryFieldName.equalsIgnoreCase(CategoryItem.NAME))
									{
										categoryItem.setName(categoryField.getFirstChild().getNodeValue());
									}
								}
							}
							categoryItems.add(categoryItem);
						}
						product.setCategories(categoryItems);
					}
					else if(productFieldName.equalsIgnoreCase(ProductItem.TAGS))
					{
						NodeList tags = productField.getChildNodes();
						
						ArrayList<TagItem> tagItems = new ArrayList<TagItem>();
						
						for(int k = 0; k < tags.getLength(); ++k)
						{
							Node tag = tags.item(k);
							TagItem tagItem = new TagItem();
							NodeList tagFields = tag.getChildNodes();
							
							for(int l = 0; l < tagFields.getLength(); ++l)
							{
								Node tagField = tagFields.item(l);
								Node tagFieldChild = tagField.getFirstChild();
								String tagFieldName = tagField.getNodeName();
								
								if(tagFieldChild != null)
								{
									if(tagFieldName.equalsIgnoreCase(CategoryItem.KEY))
									{
										tagItem.setKey(tagField.getFirstChild().getNodeValue());
									}
									else if(tagFieldName.equalsIgnoreCase(CategoryItem.NAME))
									{
										tagItem.setName(tagField.getFirstChild().getNodeValue());
									}
								}
							}
							tagItems.add(tagItem);
						}
						product.setTags(tagItems);
					}
					else if(productFieldName.equalsIgnoreCase(ProductItem.LOCATIONS))
					{
						NodeList locations = productField.getChildNodes();
						
						ArrayList<LocationItem> locationItems = new ArrayList<LocationItem>();
						
						for(int k = 0; k < locations.getLength(); ++k)
						{
							Node location = locations.item(k);
							LocationItem locationItem = new LocationItem();
							NodeList locationFields = location.getChildNodes();
							
							for(int l = 0; l < locationFields.getLength(); ++l)
							{
								Node locationField = locationFields.item(l);
								Node locationFieldChild = locationField.getFirstChild();
								String locationFieldName = locationField.getNodeName();
								
								if(locationFieldChild != null)
								{
									if(locationFieldName.equalsIgnoreCase(LocationItem.KEY))
									{
										locationItem.setKey(locationField.getFirstChild().getNodeValue());
									}
									else if(locationFieldName.equalsIgnoreCase(LocationItem.NAME))
									{
										locationItem.setName(locationField.getFirstChild().getNodeValue());
									}
									else if(locationFieldName.equalsIgnoreCase(LocationItem.ADDRESS))
									{
										//Node addressNode = locationField.getFirstChild();
										NodeList addressFields = locationField.getChildNodes();
										
										AddressItem address = new AddressItem();
										
										for(int m = 0; m < addressFields.getLength(); ++m)
										{
											Node addressField = addressFields.item(m);
											Node addressFieldChild = addressField.getFirstChild();
											if(addressFieldChild != null)
											{
												String addressFieldName = addressField.getNodeName();
												
												if(addressFieldName.equalsIgnoreCase(AddressItem.CITY))
												{
													address.setCity(addressField.getFirstChild().getNodeValue());
												}
												else if(addressFieldName.equalsIgnoreCase(AddressItem.COUNTRY))
												{
													address.setCountry(addressField.getFirstChild().getNodeValue());								
												}
												else if(addressFieldName.equalsIgnoreCase(AddressItem.NUMBER))
												{
													address.setNumber(addressField.getFirstChild().getNodeValue());
												}
												else if(addressFieldName.equalsIgnoreCase(AddressItem.POSTAL_CODE))
												{
													address.setPostalCode(addressField.getFirstChild().getNodeValue());
												}
												else if(addressFieldName.equalsIgnoreCase(AddressItem.STATE))
												{
													address.setState(addressField.getFirstChild().getNodeValue());
												}
												else if(addressFieldName.equalsIgnoreCase(AddressItem.STREET))
												{
													address.setStreet(addressField.getFirstChild().getNodeValue());
												}
												else if(addressFieldName.equalsIgnoreCase(AddressItem.TOWNSHIP))
												{
													address.setTownship(addressField.getFirstChild().getNodeValue());
												}
											}
										}
										
										locationItem.setAddress(address);
									}
									else if(locationFieldName.equalsIgnoreCase(LocationItem.GEO_POINT))
									{
										//Node geoPtNode = locationField.getFirstChild();
										NodeList geoPtFields = locationField.getChildNodes();
										
										GeoPointItem geoPoint = new GeoPointItem();
										
										for(int m = 0; m < geoPtFields.getLength(); ++m)
										{
											Node geoPtField = geoPtFields.item(m);
											String geoPtFieldName = geoPtField.getNodeName();
											if(geoPtFieldName.equalsIgnoreCase(GeoPointItem.LATITUDE))
											{
												geoPoint.setLatitude(Double.parseDouble(geoPtField.getFirstChild().getNodeValue()));
											}
											else if(geoPtFieldName.equalsIgnoreCase(GeoPointItem.LONGITUDE))
											{
												geoPoint.setLongitude(Double.parseDouble(geoPtField.getFirstChild().getNodeValue()));
											}
										}
										locationItem.setGeoPt(geoPoint);
									}
									else if(locationFieldName.equalsIgnoreCase(LocationItem.OPENING_HRS))
									{
										locationItem.setOpeningHours(locationField.getFirstChild().getNodeValue());
									}
								}
							}
							locationItems.add(locationItem);
						}
						product.setLocations(locationItems);
					}
					
					
					/*
					else if(partnerFieldName.equalsIgnoreCase(PartnerItem.CONTACT))
					{
						NodeList contactFields = partnerField.getChildNodes();
						
						ContactItem contact = new ContactItem();
						
						for(int k = 0; k < contactFields.getLength(); ++k)
						{
							Node contactField = contactFields.item(k);
							Node contactFieldChild = contactField.getFirstChild();
							if(contactFieldChild != null)
							{
								String timePeriodFieldName = contactField.getNodeName();
								if(timePeriodFieldName.equalsIgnoreCase(ContactItem.EMAIL))
								{
									contact.setEmail(contactField.getFirstChild().getNodeValue());
								}
								else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.FAX))
								{
									contact.setFax(contactField.getFirstChild().getNodeValue());
								}
								else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.MOBILE))
								{
									contact.setMobile(contactField.getFirstChild().getNodeValue());
								}
								else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.SITE))
								{
									contact.setSite(contactField.getFirstChild().getNodeValue());
								}
								else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.TELEPHONE))
								{
									contact.setTelephone(contactField.getFirstChild().getNodeValue());
								}
							}
						}
						partner.setContact(contact);
					}*/
				}
				
				products.add(product);
			}
		}
		catch(Exception exc)
		{
			Log.e(TAG, String.format("Exception occurred while parsing partners.\nMessage: %s", 
					(exc.getMessage() == null) ? "NULL" : exc.getMessage()));	
		}
		
		return products;
	}
	
	public ArrayList<PartnerItem> parsePartners(InputStream xmlPartners)
	{	
		ArrayList<PartnerItem> partners = new ArrayList<PartnerItem>();
	
		try
		{
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document document = docBuilder.parse(xmlPartners);
			Element rootElement = document.getDocumentElement();
			NodeList partnerItems = rootElement.getElementsByTagName("partner");
			for(int i = 0; i < partnerItems.getLength(); ++i)
			{
				PartnerItem partner = new PartnerItem();
				Node partnerItem = partnerItems.item(i);
				NodeList partnerFields = partnerItem.getChildNodes();
				for(int j = 0; j < partnerFields.getLength(); ++j)
				{
					Node partnerField = partnerFields.item(j);
					String partnerFieldName = partnerField.getNodeName();
					if(partnerFieldName.equalsIgnoreCase(PartnerItem.KEY))
					{
						partner.setKey(partnerField.getFirstChild().getNodeValue());
					}
					else if(partnerFieldName.equalsIgnoreCase(PartnerItem.NAME))
					{
						partner.setName(partnerField.getFirstChild().getNodeValue());
					}
					else if(partnerFieldName.equalsIgnoreCase(PartnerItem.DESC))
					{
						partner.setDescription(partnerField.getFirstChild().getNodeValue());
					}
					else if(partnerFieldName.equalsIgnoreCase(PartnerItem.CONTACT))
					{
						NodeList contactFields = partnerField.getChildNodes();
						
						ContactItem contact = new ContactItem();
						
						for(int k = 0; k < contactFields.getLength(); ++k)
						{
							Node contactField = contactFields.item(k);
							Node contactFieldChild = contactField.getFirstChild();
							if(contactFieldChild != null)
							{
								String timePeriodFieldName = contactField.getNodeName();
								if(timePeriodFieldName.equalsIgnoreCase(ContactItem.EMAIL))
								{
									contact.setEmail(contactField.getFirstChild().getNodeValue());
								}
								else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.FAX))
								{
									contact.setFax(contactField.getFirstChild().getNodeValue());
								}
								else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.MOBILE))
								{
									contact.setMobile(contactField.getFirstChild().getNodeValue());
								}
								else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.SITE))
								{
									contact.setSite(contactField.getFirstChild().getNodeValue());
								}
								else if(timePeriodFieldName.equalsIgnoreCase(ContactItem.TELEPHONE))
								{
									contact.setTelephone(contactField.getFirstChild().getNodeValue());
								}
							}
						}
						partner.setContact(contact);
					}
				}
				
				partners.add(partner);
			}
		}
		catch(Exception exc)
		{
			Log.e(TAG, String.format("Exception occurred while parsing partners.\nMessage: %s", 
					(exc.getMessage() == null) ? "NULL" : exc.getMessage()));	
		}
		
		return partners;
	}
	
	/*
	public List<Message> parse() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        List<Message> messages = new ArrayList<Message>();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document dom = builder.parse(this.getInputStream());
            Element root = dom.getDocumentElement();
            NodeList items = root.getElementsByTagName(ITEM);
            for (int i=0;i<items.getLength();i++){
                Message message = new Message();
                Node item = items.item(i);
                NodeList properties = item.getChildNodes();
                for (int j=0;j<properties.getLength();j++){
                    Node property = properties.item(j);
                    String name = property.getNodeName();
                    if (name.equalsIgnoreCase(TITLE)){
                        message.setTitle(property.getFirstChild().getNodeValue());
                    } else if (name.equalsIgnoreCase(LINK)){
                        message.setLink(property.getFirstChild().getNodeValue());
                    } else if (name.equalsIgnoreCase(DESCRIPTION)){
                        StringBuilder text = new StringBuilder();
                        NodeList chars = property.getChildNodes();
                        for (int k=0;k<chars.getLength();k++){
                            text.append(chars.item(k).getNodeValue());
                        }
                        message.setDescription(text.toString());
                    } else if (name.equalsIgnoreCase(PUB_DATE)){
                        message.setDate(property.getFirstChild().getNodeValue());
                    }
                }
                messages.add(message);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } 
        return messages;
    }
	*/
}
